package com.copyctrl.campus.portal.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.campus.portal.dto.OrderDTO;
import com.copyctrl.campus.portal.service.OrderService;
import com.copyctrl.model.CampOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService service;

    @PostMapping("/{order}")
    public ApiResponse add(@PathVariable String order) {
        CampOrder campOrder = JSONUtil.toBean(order, CampOrder.class);
        int i = service.addOrder(campOrder);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("失败");
    }

    @DeleteMapping("/{id}")
    public ApiResponse delete(@PathVariable Integer id) {
        int i = service.deleteOrder(id);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("失败");
    }

    @GetMapping("/{uid}")
    public ApiResponse find(@PathVariable Integer uid){
        Map<String, List<OrderDTO>> byUid = service.findByUid(uid);
        return ApiResponse.ofSuccess(byUid);
    }

    @PutMapping("/{order}")
    public ApiResponse update(@PathVariable String order) {
        CampOrder campOrder = JSONUtil.toBean(order, CampOrder.class);
        int i = service.updateOrder(campOrder);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("失败");
    }
}
